package com.timmy.chi.sql;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.timmy.chi.model.beans.SqlGoods;
import com.timmy.chi.model.beans.User;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Timmy on 2017/3/19.
 */

public class SqlDao {

    SqlHper mSqlHper;

    private static SqlDao instance;

    public static SqlDao getInstance() {
        if (instance == null) {
            instance = new SqlDao();
        }

        return instance;
    }

    public SqlDao init(Context context) {
        if (mSqlHper == null) {
            mSqlHper = new SqlHper(context);
        }

        return this;
    }

    public long insert(DataBean bean) {

        //1. 得到数据库对象
        SQLiteDatabase db = mSqlHper.getWritableDatabase();

        //2. 准备数据
        ContentValues values = new ContentValues();
        values.put(BlackListDB.TableBlackList.COLUMN_USERNAME, bean.userName);
        values.put(BlackListDB.TableBlackList.COLUMN_MONEY, bean.money);
        values.put(BlackListDB.TableBlackList.COLUMN_ORDER, bean.order);
        values.put(BlackListDB.TableBlackList.COLUMN_TIME, bean.time);
        values.put(BlackListDB.TableBlackList.COLUMN_TYPE, bean.type);

        //3. 添加数据到blacklist表中。 参数三，就是要添加的数据
        long result = db.insert(BlackListDB.TableBlackList.TABLE_NAME, null, values);

        db.close();
        return result;
    }

    public long insert(User bean) {

        //1. 得到数据库对象
        SQLiteDatabase db = mSqlHper.getWritableDatabase();

        //2. 准备数据
        ContentValues values = new ContentValues();
        values.put(BlackListDB.TableBlackList.COLUMN_USERNAME, bean.name);
        values.put(BlackListDB.TableBlackList.COLUMN_PASSWORD, bean.password);

        //3. 添加数据到blacklist表中。 参数三，就是要添加的数据
        long result = db.insert(BlackListDB.TableBlackList.USER_TABLE_NAME, null, values);

        db.close();
        return result;
    }

    public long insert(SqlGoods bean) {

        //1. 得到数据库对象
        SQLiteDatabase db = mSqlHper.getWritableDatabase();

        //2. 准备数据
        ContentValues values = new ContentValues();
        values.put(BlackListDB.TableBlackList.COLUMN_GOODS_NAME, bean.goods_name);
        values.put(BlackListDB.TableBlackList.COLUMN_GOODS_PRICE, bean.goods_price);
        values.put(BlackListDB.TableBlackList.COLUMN_GOODS_PIC, bean.goods_pic);
        values.put(BlackListDB.TableBlackList.COLUMN_GOODS_MATERIAL, bean.goods_material);
        values.put(BlackListDB.TableBlackList.COLUMN_TYPE, bean.type);

        //3. 添加数据到blacklist表中。 参数三，就是要添加的数据
        long result = db.insert(BlackListDB.TableBlackList.TABLE_NAME_GOODS, null, values);

        db.close();
        return result;
    }

    /**
     * @param order
     * @return 影响的行数， 0表明删除失败，否则成功
     */
    public int delete(String order) {

        //1. 得到数据库对象
        SQLiteDatabase db = mSqlHper.getWritableDatabase();

        //2. 执行删除，根据number去删除
        int result = db.delete(BlackListDB.TableBlackList.TABLE_NAME,
                BlackListDB.TableBlackList.COLUMN_USERNAME + "=?", new String[]{order});

        db.close();
        return result;
    }

    /**
     * 更新黑名单
     *
     * @return 影响的行数，如果是0表明失败，否则成功
     */
    public int update(String order, String username) {

        //1. 得到数据库对象
        SQLiteDatabase db = mSqlHper.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(BlackListDB.TableBlackList.COLUMN_ORDER, order);

        int result = db.update(BlackListDB.TableBlackList.TABLE_NAME, values, "username=?", new String[]{username});

        db.close();

        return result;
    }

    /**
     * @return List<BlackListBean>
     */
    public List<DataBean> query() {

        List<DataBean> list = new ArrayList<DataBean>();
        SQLiteDatabase db = mSqlHper.getReadableDatabase();

        String[] projection = {
                BlackListDB.TableBlackList.COLUMN_TYPE
        };

        // _id  ,number , type
        Cursor cursor = db.query(BlackListDB.TableBlackList.TABLE_NAME, projection, null, null, null, null, null);


        while (cursor.moveToNext()) {

            DataBean bean = null;

            bean.userName = cursor.getString(1);
            bean.money = cursor.getDouble(1);
            bean.order = cursor.getString(1);
            bean.time = cursor.getLong(1);
            bean.type = cursor.getInt(1);

            list.add(bean);
        }
        cursor.close();
        db.close();

        return list;
    }


}
